using System.Collections.Generic;
using System.Linq;
using DataObject.Core;
using DataObject.Database;

namespace DataObject.DataAccess
{
    public class BoatDAO : DataBaseProcessBase<BoatEntity, OBSDataContext>
    {
        #region CreateInstant

        public static BoatDAO CreateInstant()
        {
            return new BoatDAO();
        }

        #endregion

        public List<BoatEntity> GetAllBoats()
        {
            using (var db = new OBSDataContext(connectionString))
            {
                return db.BoatEntities.Where(x => x.Active).ToList();
            }
        }

        public List<BoatEntity> FindBoat(string boatID, string boatName, int? numOfCabins, int? numOfFloors,
            bool? active, HashSet<string> sortClause)
        {
            int count;
            return FindBoat(boatID, boatName, numOfCabins, numOfFloors, active, null, null, out count, sortClause);
        }

        public List<BoatEntity> FindBoat(string boatID, string boatName, int? numOfCabins, int? numOfFloors,
            bool? active, int? page, int? row, out int count, HashSet<string> sortClause)
        {
            using (var db = new OBSDataContext(connectionString))
            {
                IQueryable<BoatEntity> query = (from table in db.BoatEntities select table);
                if (!boatID.Equals(string.Empty))
                    query = query.Where(table => table.BoatID.ToLower().Equals(boatID.ToLower()));
                if (!boatName.Equals(string.Empty))
                    query = query.Where(table => table.BoatName.ToLower().Equals(boatName.ToLower()));
                if (numOfCabins.HasValue)
                    query = query.Where(table => table.NumOfCabins == numOfCabins.Value);
                if (numOfFloors.HasValue)
                    query = query.Where(table => table.NumOfFloors == numOfFloors.Value);
                if (active.HasValue)
                    query = query.Where(table => table.Active == active.Value);
                for (int i = sortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(sortClause.ElementAt(i));
                count = query.Count();
                if (page.HasValue && row.HasValue)
                    return query.PageIQuery(page.Value, row.Value).ToList();
                return query.ToList();
            }
        }

        public BoatEntity GetBoat(string boatID)
        {
            using (var db = new OBSDataContext(connectionString))
            {
                return db.BoatEntities.SingleOrDefault(a => a.BoatID.Equals(boatID));
            }
        }

        public void InsertBoats(List<BoatEntity> ls)
        {
            using (var db = new OBSDataContext(connectionString))
            {
                db.BoatEntities.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }

        public void UpdateBoat(BoatEntity item)
        {
            using (var db = new OBSDataContext(connectionString))
            {
                // db.UpdateBoat(item.BoatID, item.BoatName, item.NumOfCabins, item.NumOfFloors, item.Active, item.Color);
                db.UpdateBoat(item.BoatID, item.BoatName, item.NumOfCabins, item.NumOfFloors, item.Active, item.Color);
            }
        }
    }
}